//
//  KLSlidingViewController.m
//  KeyLime
//
//  Created by Jesse Curry on 9/1/09.
//  Copyright 2009 Circonda, Inc. All rights reserved.
//

#import "KLSlidingViewController.h"

#define OFFSCREEN_Y		((CGRectGetHeight( self.view.frame ) / 2) + 480)
#define ONSCREEN_Y		(480 - (CGRectGetHeight( self.view.frame ) / 2))

NSString* const kSlideOnAnimationIdentifier = @"KLSlidingIntegerInput_SlideOn";
NSString* const kSlideOffAnimationIdentifier = @"KLSlidingIntegerInput_SlideOff";

@implementation KLSlidingViewController
@synthesize delegate;
@synthesize isDisplayed = viewDisplayed;

/* // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName: (NSString*)nibNameOrNil bundle: (NSBundle*)nibBundleOrNil 
{
    if ( self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil] ) 
	{
        // Custom initialization
    }
    return self;
}*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	// Add this view to the main window
	[[UIApplication sharedApplication].keyWindow addSubview: self.view];
	[[self view] setCenter: CGPointMake( 160, OFFSCREEN_Y )];
}

- (IBAction)done: (id)sender
{
	[delegate klSlidingViewControllerDidFinish: self];
	[self slideOff];
}

- (IBAction)cancel: (id)sender
{
	if ( [delegate respondsToSelector: @selector(klSlidingViewControllerDidCancel:)] )
		[delegate klSlidingViewControllerDidCancel: self];
	
	[self slideOff];
}
 
/**
 @brief Slides the view onto the screen from the bottom up.
 */
- (void)slideOn
{
	if ( viewDisplayed )
		return;
	
	[titleLabel setText: [self title]];
	
	[UIView beginAnimations: kSlideOnAnimationIdentifier context: NULL];
	[UIView setAnimationDelegate: self];
	[UIView setAnimationDidStopSelector: 
	 @selector(slideOnAnimationDidStop:finished:context:)];
	
	[UIView setAnimationDuration: 0.3];
	[[self view] setCenter: CGPointMake( 160, ONSCREEN_Y )];
	[UIView commitAnimations];

	viewDisplayed = YES;
	
	if ( [delegate respondsToSelector: @selector(klSlidingViewControllerWillAppear:)] )
		[delegate klSlidingViewControllerWillAppear: self];
}

	 
/**
 @brief Slides the view downwards and off the screen.
 */
- (void)slideOff
{
	if ( !viewDisplayed )
		return;

	[UIView beginAnimations: kSlideOffAnimationIdentifier context: NULL];
	[UIView setAnimationDelegate: self];
	[UIView setAnimationDidStopSelector: 
	 @selector(slideOffAnimationDidStop:finished:context:)];
	
	[UIView setAnimationDuration: 0.3];
	[[self view] setCenter: CGPointMake( 160, OFFSCREEN_Y )];
	[UIView commitAnimations];
	
	if ( [delegate respondsToSelector: @selector(klSlidingViewControllerWillDisappear:)] )
		[delegate klSlidingViewControllerWillDisappear: self];	
}

#pragma mark -
#pragma mark Animation Delegates
- (void)slideOnAnimationDidStop: (NSString*)animationID 
					   finished: (NSNumber*)finished 
						context: (void*)context
{
	if ( [delegate respondsToSelector: @selector(klSlidingViewControllerDidAppear:)] )
		[delegate klSlidingViewControllerDidAppear: self];
}

- (void)slideOffAnimationDidStop: (NSString*)animationID 
						finished: (NSNumber*)finished 
						 context: (void*)context
{
	viewDisplayed = NO;
	if ( [delegate respondsToSelector: @selector(klSlidingViewControllerDidDisappear:)] )
		[delegate klSlidingViewControllerDidDisappear: self];
}

#pragma mark -
- (void)didReceiveMemoryWarning 
{
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload 
{
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc 
{
	[self.view removeFromSuperview];
    [super dealloc];
}


@end
